package tasks

import contributors.*
import java.util.Collections.emptyList

suspend fun loadContributorsProgress(
    service: GitHubService,
    req: RequestData,
    updateResults: suspend (List<User>, completed: Boolean) -> Unit
) {
    val repos = service.getOrgRepos(req.org)
        .also { logRepos(req, it) }
        .body() ?: emptyList()
    val users = mutableListOf<User>()
    repos.forEachIndexed { index, repo ->
        users.addAll(
            service.getRepoContributors(req.org, repo.name)
                .also { logUsers(repo, it) }
                .body() ?: emptyList()
        )
        updateResults(users.aggregate(), repos.lastIndex == index)
    }
}
